home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / stat.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  3KB  |  127 lines

  1.  
  2. /*
  3.  *  STAT call
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <dos/dos.h>
  12. #include <dos/dosextens.h>
  13. #include <clib/dos_protos.h>
  14. #include <sys/stat.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <lib/misc.h>
  20.  
  21. #ifndef UnixToAmigaPath
  22. #define UnixToAmigaPath(path)    path
  23. #endif
  24.  
  25.  
  26. typedef struct FileInfoBlock    FileInfoBlock;
  27.  
  28. stat(name, stat)
  29. const char *name;
  30. struct stat *stat;
  31. {
  32.     __aligned FileInfoBlock fib;
  33.     BPTR lock;
  34.     int r = -1;
  35.  
  36.     clrmem(stat, sizeof(*stat));
  37.     /*
  38.      *    If lock fails find file via its parent directory.  This is for
  39.      *    unix compatibility because you can stat an open write file in unix.
  40.      */
  41.  
  42.     fib.fib_FileName[0] = 0;
  43.  
  44.     if ((lock = Lock(UnixToAmigaPath(name), SHARED_LOCK)) == NULL) {
  45.     char *buf = strdup(UnixToAmigaPath(name));
  46.     char *ptr;
  47.     char sk = 0;
  48.  
  49.     for (ptr = buf + strlen(buf); ptr >= buf && *ptr != ':' && *ptr != '/'; --ptr);
  50.     if (ptr < buf || *ptr == ':') {
  51.         ++ptr;
  52.         sk = *ptr;
  53.     }
  54.     *ptr = 0;
  55.     lock = Lock(buf, SHARED_LOCK);
  56.     if (sk)
  57.         *ptr = sk;
  58.     else
  59.         ++ptr;
  60.  
  61.     if (lock == NULL) {
  62.         free(buf);
  63.         errno = ENOENT;
  64.         return(-1);
  65.     }
  66.     if (Examine(lock, &fib)) {
  67.         while (ExNext(lock, &fib)) {
  68. #ifdef TEST
  69.         printf("Compare '%s' '%s'\n", ptr + 1, fib.fib_FileName);
  70. #endif
  71.         if (stricmp(ptr, fib.fib_FileName) == 0) {
  72.             r = 0;
  73.             break;
  74.         }
  75.         }
  76.     }
  77.     free(buf);
  78.     } else {
  79.     if (Examine(lock, &fib))
  80.         r = 0;
  81.     }
  82.     if (lock == NULL) {
  83.     errno = ENOENT;
  84.     return(-1);
  85.     }
  86.     if (r >= 0) {
  87.     stat->st_size = fib.fib_Size;
  88.     stat->st_ino = (long)((struct FileLock *)BADDR(lock))->fl_Key;
  89.     stat->st_dev = (long)((struct FileLock *)BADDR(lock))->fl_Task;
  90.     stat->st_mode = (fib.fib_DirEntryType > 0) ? S_IFDIR : S_IFREG;
  91.     stat->st_ctime = stat->st_mtime = fib.fib_Date.ds_Days * (1440 * 60) +
  92.                     fib.fib_Date.ds_Minute * 60 +
  93.                     fib.fib_Date.ds_Tick / 50 + _TimeCompensation;
  94.     if ((fib.fib_Protection & 8) == 0)
  95.         stat->st_mode |= S_IREAD;
  96.     if ((fib.fib_Protection & 4) == 0)
  97.         stat->st_mode |= S_IWRITE;
  98.     if ((fib.fib_Protection & 2) == 0)
  99.         stat->st_mode |= S_IEXEC;
  100.     if (fib.fib_Protection & 0x40)
  101.         stat->st_mode |= S_IEXEC;
  102.     }
  103.     UnLock(lock);
  104.     if (r < 0)
  105.     errno = ENOENT;
  106.     return(r);
  107. }
  108.  
  109. #ifdef TEST
  110.  
  111. #include <fcntl.h>
  112.  
  113. main(ac, av)
  114. char *av[];
  115. {
  116.     struct stat xstat;
  117.     short i;
  118.  
  119.     for (i = 1; i < ac; ++i) {
  120.     int r = stat(av[i], &xstat);
  121.     printf("r = %d fs=%d ti=%08lx\n", r, xstat.st_size, xstat.st_ctime);
  122.     }
  123.     return(0);
  124. }
  125.  
  126. #endif
  127.